package com.prj.ufdm.file.service._impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.gridfs.GridFsOperations;
import org.springframework.data.mongodb.gridfs.GridFsResource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
import com.mongodb.client.gridfs.model.GridFSFile;
import com.prj.ufdm.file.dao.AloneFileGridFsDao;
import com.prj.ufdm.file.enums.MSFileConstants;
import com.prj.ufdm.file.model.AloneFile;
import com.prj.ufdm.file.service.AloneFileService;

@Service
public class AloneFileServiceImpl implements AloneFileService {

	@Autowired
	private GridFsOperations gridFsOperations;
	@Autowired
	private AloneFileGridFsDao aloneFileGridFsDao;

	
    /**
     * DESC: 保存文件
     * @param file
     * @param aloneFile 
     * @return
     * @throws Exception
     */
	@Override
	public AloneFile saveAloneFile(MultipartFile file,AloneFile aloneFile) throws Exception{
		
		DBObject metaData = new BasicDBObject();
		metaData.put("fileHash",  DigestUtils.md5Hex(file.getInputStream()));
		metaData.put("fileName", file.getOriginalFilename().lastIndexOf(".")!=-1 ? file.getOriginalFilename().substring(0,file.getOriginalFilename().lastIndexOf(".")) : file.getOriginalFilename());
		if(aloneFile.getAliases()==null || aloneFile.getAliases().equals("")) {
			aloneFile.setAliases(file.getOriginalFilename().lastIndexOf(".")!=-1 ? file.getOriginalFilename().substring(0,file.getOriginalFilename().lastIndexOf(".")) : file.getOriginalFilename());
		}
		metaData.put("aliases", aloneFile.getAliases());
		metaData.put("fileSize", String.valueOf(file.getSize()));
		metaData.put("fileType", file.getOriginalFilename().lastIndexOf(".")!=-1 ? file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1) : MSFileConstants.DEFAULT_FILE_TYPE);
		metaData.put("contentType", file.getContentType());
		GridFSFile gridFSFile = aloneFileGridFsDao.storeGridFs(file,metaData);
		AloneFile retAloneFile = gridFsToAloneFile(gridFSFile);
		return retAloneFile;
	}
	
	/**
	 * DESC: 根据HASH值获取文件
	 * @param fileHash
	 * @return
	 * @throws Exception
	 */
	public AloneFile getAloneFileByFileHash(String fileHash) throws Exception {
		GridFSFile gridFSFile = aloneFileGridFsDao.findGridFsByFileHash(fileHash);
		AloneFile aloneFile = null;
		if(gridFSFile!=null && gridFSFile.getId()!=null && !gridFSFile.getId().toString().equals("")) {
			aloneFile = gridFsToAloneFile(gridFSFile);
		}
		return aloneFile;
	}
	
    /**
     * DESC: 根据 wholeFileId 值获取所有 文件
     * @param wholeFileId
     * @return
     * @throws Exception
     */
	@Override
	public List<GridFsResource> getAllGridFsResourceByWholeFileId(String wholeFileId) throws Exception {
		List<GridFsResource> listGridFsResource = new ArrayList<GridFsResource>();
		List<GridFSFile> listGridFSFile = aloneFileGridFsDao.findAllGridFsByWholeFileId(wholeFileId);
		if(listGridFSFile!=null && listGridFSFile.size()>0) {
			for(GridFSFile gridFSFile : listGridFSFile) {
				GridFsResource resource = gridFsOperations.getResource(gridFSFile);
				listGridFsResource.add(resource);
			}
		}
		return listGridFsResource;
	}
	
    /**
     * DESC: 根据 wholeFileHash 值获取所有 文件
     * @param wholeFileId
     * @return
     * @throws Exception
     */
	@Override
	public List<GridFsResource> getAllGridFsResourceByWholeFileHash(String wholeFileHash) throws Exception {
		List<GridFsResource> listGridFsResource = new ArrayList<GridFsResource>();
		List<GridFSFile> listGridFSFile = aloneFileGridFsDao.findAllGridFsByWholeFileHash(wholeFileHash);
		if(listGridFSFile!=null && listGridFSFile.size()>0) {
			for(GridFSFile gridFSFile : listGridFSFile) {
				GridFsResource resource = gridFsOperations.getResource(gridFSFile);
				listGridFsResource.add(resource);
			}
		}
		return listGridFsResource;
	}
	
	
	public AloneFile gridFsToAloneFile(GridFSFile gridFSFile) {
		AloneFile aloneFile = new AloneFile();
		if(gridFSFile!=null && gridFSFile.getId()!=null && !gridFSFile.getId().toString().equals("")) {
			aloneFile.setId(gridFSFile.getObjectId().toString());
			aloneFile.setFileId(gridFSFile.getObjectId().toString());
			aloneFile.setFileHash(gridFSFile.getMetadata().getString("fileHash"));
			aloneFile.setFileName(gridFSFile.getMetadata().getString("fileName"));
			aloneFile.setAliases(gridFSFile.getMetadata().getString("aliases"));
			aloneFile.setFileSize(gridFSFile.getMetadata().getString("fileSize"));
			aloneFile.setFileType(gridFSFile.getMetadata().getString("fileType"));
			GridFsResource gridFsResource = gridFsOperations.getResource(gridFSFile);
			aloneFile.setGridFsResource(gridFsResource);
		}
		return aloneFile;
	}
	


}